package com.wfg.common;

import com.wfg.common.msg.Invocation;
import org.apache.commons.io.IOUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ObjectInputStream;
import java.lang.reflect.Method;

/**
 * @author wufagang
 * @description
 * @date 2023年05月11日 07:55
 */
public class HttpServerHandler {

    public void handler(HttpServletRequest req, HttpServletResponse resp){
            try {
                ObjectInputStream ois = new ObjectInputStream(req.getInputStream());
                Invocation invocation =(Invocation)ois.readObject();
                String interfaceName=invocation.getInterfaceName();
                Class implclass=LocalRegister.get(interfaceName);
                Method method=implclass.getMethod(invocation.getMethodName(),invocation.getParameterType());
                String result = (String) method.invoke(implclass.newInstance(),invocation.getArgs());
                System.out.println("tomcat:"+result);
                IOUtils.write(result,resp.getOutputStream());
            }catch(Exception e){
                e.printStackTrace();
            }


    }
}
